package parser;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.SAXException;

import java.io.IOException;

/**
 * @author 石小俊
 */
public class DigesterParser {

    public static void main(String[] args) {
        // 解析rule.xml,获取对应解析器,该解析过程由第三方实现
        Digester digester = DigesterLoader.createDigester(DigesterParser.class.getClassLoader().getResource("parser/rule.xml"));
        // 准备一个空的喜爱你个,放入到解析器中,作为根标签对应的对象
        // 当解析器将结果解析完成之后,会将数据存储到该对象中
        ClassBean classBean = new ClassBean();
        digester.push(classBean);
        try {
            System.out.println("解析前:" + classBean);
            // 开始解析,指定需要解析的xml文件
            digester.parse(DigesterParser.class.getClassLoader().getResourceAsStream("students.xml"));
            System.out.println("解析后:" + classBean);
            System.out.println("学号为1的学生姓名:" + classBean.findStudent("1").getName());
            System.out.println("学号为1的学生性别:" + classBean.findStudent("1").getGender());
            System.out.println("学号为2的学生姓名:" + classBean.findStudent("2").getName());
            System.out.println("学号为2的学生性别:" + classBean.findStudent("2").getGender());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

}
